/**
 *
 * @class app.widgets.TaskWall
 * @extends Ext.grid.GridPanel
 */
Ext.ns('app.widgets'); 

app.widgets.TaskWall = Ext.extend(Ext.grid.GridPanel, {
	
	/*
	 * Soft config
	 */
	
	iconCls: 'taskwall-icon' 
	
	/*
	 * Init functions: 
	 */
		
	, initComponent : function() {
        var config = this.createConfig();
    
        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);
        
    	app.widgets.TaskWall.superclass.initComponent.apply(this, arguments);
    	
    	this.initListeners(); 

    } 

	, initListeners : function(){
		this.on('rowclick', this.onRowClick, this);
	}
	
	/*
	 * Factory functions: 
	 */
	
	, createConfig : function(){ 
		
		var config = { 
			id 				: this.initialConfig.id || Ext.id() 
			, title 		: 'Task Wall'	
			, currentTask 	: false
			, loadMask		: true
		};
		
		var dateFormat = app.Utils.DateFormat.SIMPLE; 
		
		config.store = new Ext.data.Store({
			
			reader: new Ext.data.JsonReader({
				totalProperty: 'results',
				id: 'id', 
				fields: ['id',
				         'project',
				         'name',
				         'description',
				         {name: 'created_at', type: 'date', dateFormat: dateFormat},
				         {name: 'started_at', type: 'date', dateFormat: dateFormat},
				         {name: 'closed_at', type: 'date', dateFormat: dateFormat},
				         'status',
				         'is_default']
			})
		
			, proxy: new Ext.data.DirectProxy({
				directFn: TaskWall.getTasks
			}) 
		
			, sortInfo: {
				field: 'name',
				direction: 'ASC'
			}
		}); 
		
		var colId = Ext.id(); 
		
		var cm = new Ext.grid.ColumnModel({
			columns: [{id: colId, dataIndex: 'name'}]
		}); 
		
		config.cm = cm; 
		config.autoExpandColumn = colId; 
		
		config.viewConfig = {
			 enableRowBody:true
            , getRowClass : this.applyRowClass
		}; 
		
		config.tbar = new Ext.Toolbar({
			items:[
			       {iconCls:'refresh-icon',handler:this.refresh,scope:this}
			       , '-' , app.Actions.createTask()
			       , app.Actions.createProject()
			       , {iconCls:'closetask-icon',text:'Close Task',handler:this.onCloseTask,scope:this}
			       ]
		}); 
		
		this.CurrentTaskLabel = new Ext.form.Label({cls:'running-task'}); 
		
		config.bbar = new Ext.Toolbar({
			items: [
			        {xtype:'label', text: 'Running Task:'}
			        , ' ', ' ', ' '
			        , this.CurrentTaskLabel 
			       ]
		}); 
		
		return config; 
	}
	
	/*
	 * Event handlers: 
	 */
	
	, onRender : function(){
		
		app.widgets.TaskWall.superclass.onRender.apply(this, arguments);
		
		this.store.load(); 
	}

	, applyRowClass : function(record, rowIndex, p, ds) {
        
        var xf = Ext.util.Format;
        p.body = '<p>' + xf.ellipsis(xf.stripTags(record.data.description), 200) + '</p>';
        return this.id + '-row-expanded';
    
    }
	
	, refresh : function(){
		this.store.load({
			callback:this.afterRefresh, 
			scope:this
		}); 
	}
	
	, afterRefresh : function(records,options,success){
		
		if(!success || !records.length){
			this.store.removeAll(); 
		} 
		
	}
	
	, onRowClick : function(grid, index, evObj){
		var task = this.store.getAt(index); 
		this.stopNStart(task.id); 
	}

	, onCloseTask : function(){
		var task = this.getSelectionModel().getSelected();
		this.closeTask(task.id, this.closeTaskCallback);  
	}
	
	, closeTaskCallback : function(result, resObj){}

	/*
	 * Metodi per i task: 
	 */
	
	, startTask : function(taskId){
		
		TaskWall.startTask({tid: taskId},function(result,e){
			
			if(result.success){
				this.setCurrentTask(taskId);  
			} else {
				app.Msg.warn(result.msg); 
			}
			
		},this); 
	}
	
	, stopTask : function(){
		if (this.currentTask !== false){
			
			TaskWall.stopTask({tid: this.currentTask},function(result){
				
				if(result.success){
					this.setCurrentTask(false);  
				} else {
					app.Msg.warn(result.msg); 
				}
				
			}, this);
			
		}
		
	}
	
	, stopNStart : function(taskId) {
		if(taskId == this.currentTask){
			this.stopTask(); 
		} else if (this.currentTask != false){
			this.stopTask();
			this.startTask(taskId);
		} else {
			this.startTask(taskId); 
		}
	}
	
	, closeTask : function(taskId){
		if(taskId == this.currentTask){
			this.stopTask(taskId); 
		}
		
		TaskWall.closeTask({tid : taskId});
	}
	
	/*
	 * Getters & Setters:
	 */
	
	, setCurrentTask : function(taskId){
		this.currentTask = taskId;

		if(taskId !== false){
			var record = this.store.getById(taskId);
			var task = record ? record.data.name : taskId + ' (id)'; 
			this.CurrentTaskLabel.setText(task);
		} else {
			this.CurrentTaskLabel.setText('');
		}

	}
	
	, getCurrentTask : function(){
		return this.currentTask; 
	}
	
}); 
 

Ext.reg('taskwall', app.widgets.TaskWall); 
 
// eof
